import axios from 'axios';

const personRequest = axios.create({
  baseURL: '/api',
  withCredentials: false,
  timeout: 30 * 1000,
});

personRequest.interceptors.request.use(
  (config) => {
    config.headers['Content-Type'] = 'application/json';
    return config;
  },
  (error) => Promise.reject(error)
);

personRequest.interceptors.response.use(
  (res) => {
    console.log(res);
    const status = res.status
    if (status == 200) {
      if (res.data.status == "success") {
        return res.data;
      } else {
        return Promise.reject(new Error(res.data.message))
      }
    } else {
      return Promise.reject(new Error(res.status));
    }
  },
  (error) => {
    return Promise.reject(new Error(error));
  }
);

export default personRequest;
